﻿using System;
using System.IO;
using UnityEngine;
using Utils;
using Utils.FileFolderModule;
using Utils.JsonModule;

public class ArchiveController
{
    public LevelData levelData = new LevelData();

    public LevelData TempLevelData { get; set; }

    public GameObject mapRoot, playerRoot;

    public ArchiveController()
    {
        mapRoot = UITool.GetGameObjectByPath("", GameController.Instance().mapCtr.gameObject);
        playerRoot = UITool.GetGameObjectByPath("", GameController.Instance().playerCtr.gameObject);
        if (!Directory.Exists(FilePathConfigue.fileRoot))
        {
            FolderHandler.CreateFolder(FilePathConfigue.fileRoot);
        }
    }

    public void CreateNewArchive()
    {
        levelData.gameType = GameType.Dungeon;
        levelData.state = false;
        string levelDataJson = JsonHandler.GetJsonFromObject<LevelData>(levelData);
        FileHandler.ReWriteFile(FilePathConfigue.LevelData, levelDataJson);
        
        LoadArchive();
    }

    public void RefreshNewLevelData()
    {
        string levelDataJson = JsonHandler.GetJsonFromObject<LevelData>(new LevelData());
        FileHandler.ReWriteFile(FilePathConfigue.LevelData, levelDataJson);
    }

    public void CreateStoryArchive()
    {
        // levelData.gameType = GameType.Story;
        levelData.state = false;
        string levelDataJson = JsonHandler.GetJsonFromObject<LevelData>(levelData);
        FileHandler.ReWriteFile(FilePathConfigue.LevelData, levelDataJson);

        LoadArchive();
    }

    public void LoadArchive()
    {
        TempLevelData = JsonHandler.GetObjectFromJson<LevelData>(FilePathConfigue.LevelData);
        GenerateMaps();
        GameController.Instance().smallMap =
            PrefabRoot.SmallMap.GeneratePrefab(GameController.Instance().gameUIRoot.gameObject).GetComponent<SmallMapBase>();
        if (TempLevelData.gameType == GameType.Dungeon)
        {
            GeneratePlayer();
        }
        else if (TempLevelData.gameType == GameType.Story)
        {
            // GenerateStoryMap();
            GeneratePlayerStory();
        }
    
        GenerateWeapon();
        GameController.Instance().Initialize();
        GameController.Instance().FSM.CurGameState = GameState.Gaming;
    }

    public void SaveArchive()
    {
        if (TempLevelData.gameType == GameType.Story)
        {
            TempLevelData.weapon = GameController.Instance().weaponCtr.Weapon.self;
        }

        string levelDataJson = JsonHandler.GetJsonFromObject<LevelData>(TempLevelData);
        FileHandler.ReWriteFile(FilePathConfigue.LevelData, levelDataJson);
    }

    private void Generating()
    {
    }

    public void GenerateMaps()
    {
        int i = 0;
        // if (GameController.Instance().archiveCtr.TempLevelData.gameType == GameType.Story)
        // {
        //     i = 1;
        // }
        for (; i < TempLevelData.mapList.Count; i++)
        {
            GameObject go = TempLevelData.mapList[i].GeneratePrefab(mapRoot);
            GameController.Instance().mapCtr.mapList.Add(go);
            if (i == TempLevelData.Level)
            {
                go.SetActive(true);
                GameController.Instance().mapCtr.mapIndex = i;
            }
        }
    }

    private void GenerateStoryMap()
    {
        GameObject go = TempLevelData.mapList[0].GeneratePrefab(mapRoot);
        GameController.Instance().mapCtr.mapList.Add(go);
    }

    private void GeneratePlayer()
    {
        GameObject go = TempLevelData.player.GeneratePrefab(playerRoot);
        Vector3 pos = UITool.GetChildGameObject("StartPoint",
                GameController.Instance().mapCtr.mapList[GameController.Instance().archiveCtr.TempLevelData.Level])
            .transform.position;
        GameController.Instance().playerCtr.transform.position = pos;
        playerRoot.SetActive(true);
    }

    private void GeneratePlayerStory()
    {
        GameObject go = TempLevelData.player.GeneratePrefab(playerRoot);
        playerRoot.SetActive(true);
    }

    private void GenerateWeapon()
    {
        GameObject go = TempLevelData.weapon.GeneratePrefab(playerRoot);
        Weapon weapon = go.GetComponent<Weapon>();
        weapon.equipped = true;
        weapon.canPick = false;
        weapon.GetComponent<Collider2D>().enabled = false;
        GameController.Instance().weaponCtr.Weapon = weapon;
        go.SetActive(true);
    }
}